<!---
to do list
Permissions

--->

<cfparam name="Site_ID" type="integer" default="0" />
<cfparam name="returnView" type="string" default="sites" />
<cfparam name="passback" type="string" default="" />

<cfscript>
	siteGateway = new assets.calgrants.com.gateway.SiteGateway();
	site = siteGateway.get(Site_ID = Site_ID);
</cfscript>

<cfif IsNumeric(site.getSite_ID())>

	<cfscript>
		base = new assets.calgrants.com.Base();
		passbackQueryString = base.passbackToQueryString(passback);
		organizationGateway = new assets.calgrants.com.gateway.OrganizationGateway();
		organizations = organizationGateway.query(regionIDs = IIf(IsUserInRole("Regional Admin"), request.authUser.regionID, ""), Organization_ID = IIf(IsUserInRole("Organizer") OR IsUserInRole("Site Contact"), request.authUser.organizationID, ""));
		countyGateway = new assets.calgrants.com.gateway.CountyGateway();
		counties = countyGateway.query(Region_ID = IIf(IsUserInRole("Organizer") OR IsUserInRole("Site Contact"), request.authUser.regionID, ""));
	</cfscript>
	
	<script type="text/javascript" language="javascript">
	<!--
		<cfoutput>
			var siteID = #site.getSite_ID()#;
			var organizationID = #site.getOrganization_ID()#;
			var siteType = "#site.getSite_Type()#";
			var countyID = #site.getCounty_ID()#;
			var computers = "#site.getComputers()#";
				
			var organizations = new kendo.data.DataSource();
			organizations.data(#SerializeJSON(base.QueryToStruct(organizations), false)#);
			
			var siteTypes = new kendo.data.DataSource();
			siteTypes.data([{site_type: "High School", description: "Public or private secondary school"},{site_type: "College Campus", description: "University, college, community college, etc."},{site_type: "Community Partner", description: "Any non-school location"}]);
			
			var counties =  new kendo.data.DataSource();
			counties.data(#SerializeJSON(base.QueryToStruct(counties), false)#);
			
			var computersOptions = new kendo.data.DataSource();
			computersOptions.data([{computers: "Less than 10"},{computers: "10 to 20"},{computers: "21 to 50"},{computers: "More than 50"},{computers: "None"}]);
		</cfoutput>	
		
		var validate = function() {
			var errors = "";
			var valid = true;
			//validation logic
			if (!valid) {
				alert(errors);
			}
			return valid;				
		}
		$(function() {
			organizationDropdown = $("#Organization_ID").kendoDropDownList({
				dataTextField: "organization",
				dataValueField: "organization_id",
				dataSource: organizations,
				template: 	'<strong>${organization}</strong><br>' +
							'${address}<br>' +
							' # if (data.city.length > 0) { # ' +
							'${city}, CA ${zip_code}<br>' +
							' # } # '
			}).data("kendoDropDownList");
			organizationDropdown.value(organizationID);
			siteTypeDropdown = $("#Site_Type").kendoDropDownList({
				dataTextField: "site_type",
				dataValueField: "site_type",
				dataSource: siteTypes,
				template: 	'<strong>${site_type}</strong><br>' +
							'${description}'
			}).data("kendoDropDownList");
			siteTypeDropdown.value(siteType);
			countyDropdown = $("#County_ID").kendoDropDownList({
				dataTextField: "county",
				dataValueField: "county_id",
				dataSource: counties,
				template: 	'<strong>${county}</strong><br>' +
							'${region_id} - ${region} Region'
			}).data("kendoDropDownList");
			countyDropdown.value(countyID);
			computersDropdown = $("#Computers").kendoDropDownList({
				dataTextField: "computers",
				dataValueField: "computers",
				dataSource: computersOptions
			}).data("kendoDropDownList");
			computersDropdown.value(computers);
			$("#siteModal").modal();
			resizeModal("siteModal");
		})
	//-->
	</script>
	
	<cfoutput>
	
		<cfform id="siteForm" name="siteForm" action="#script_name#" method="post" onsubmit="return validate();">
			<cfinput type="hidden" name="action" value="saveSite" />
			<cfinput type="hidden" name="view" value="#returnView#" />
			<cfinput type="hidden" name="Site_ID" value="#site.getSite_ID()#" />
			<cfif IsDefined("passback")>
				<cfloop index="keyValue" list="#passback#" delimiters=";">
					<cfinput type="hidden" name="#ListFirst(keyValue)#" value="#ListLast(keyValue)#" />
				</cfloop>
			</cfif>
			
			<div id="siteModal" class="modal modal-fullscreen" data-backdrop="static">
				<div class="modal-header">
					<h3>Site</h3>
				</div>
				<div class="modal-body">
					<div class="container-fluid">
						<div class="row-fluid">
							<div class="span4">
								<div class="kendo-control clearfix">
									<label class="required">Organization</label>
									<cfselect name="Organization_ID" required="yes" message="Organization is required" class="span9" style="margin: 0;"></cfselect>
								</div>
								<div class="clearfix">
									<label class="required">Site</label>
									<cfinput type="text" name="Site" value="#site.getSite()#" required="yes" message="Site is required" maxlength="200" class="span9">
								</div>
								<div class="kendo-control clearfix">
									<label class="required">Site Type</label>
									<cfselect name="Site_Type" required="yes" message="Site Type is required" class="span9" style="margin: 0;"></cfselect>
								</div>
								<div class="clearfix">
									<label class="required">Address</label>
									<cfinput type="text" name="Address" value="#site.getAddress()#" required="yes" message="Address is required" maxlength="200" class="span9">
								</div>
								<div class="clearfix">
									<label class="required">City</label>
									<cfinput type="text" name="City" value="#site.getCity()#" required="yes" message="City is required" maxlength="100" class="span9">
								</div>
								<div class="clearfix">
									<label class="required">Zip Code</label>
									<cfinput type="text" name="Zip_Code" value="#site.getZip_Code()#" required="yes" message="Zip Code is required and must be a valid zip code" validate="zipcode" maxlength="5" class="span9">
								</div>
								<div class="kendo-control clearfix">
									<label class="required">County</label>
									<cfselect name="County_ID" required="yes" message="County is required" class="span9" style="margin: 0;"></cfselect>
								</div>
								<div class="kendo-control clearfix">
									<label class="required">Computers</label>
									<cfselect name="Computers" required="yes" message="Computers is required" class="span9" style="margin: 0;"></cfselect>
								</div>
								<!--- <cfif site.getSite_ID() gt 0>
									<div class="text-control clearfix">
										<label>ID</label>
										#site.getSite_ID()#
									</div>
									<div class="text-control clearfix">
										<label>Date Created</label>
										#DateFormat(site.getDate_Created(), "mmmm d yyyy")# @ #TimeFormat(site.getDate_Created(), "h:mmtt")#
									</div>
								</cfif> --->
							</div>
							<div class="span8">
							
								<!--- <cfif site.getSite_ID() gt 0>
							
									<div class="accordion" id="siteAccordian">
										<div class="accordion-group">
											<div class="accordion-heading">
												<a class="accordion-toggle" data-toggle="collapse" data-parent="##siteAccordian" href="##panel1">Users</a>
											</div>
											<div id="panel1" class="accordion-body collapse">
												<div class="accordion-inner">
													<cfinclude template="#request.pathToUI#/lists/users.cfm">
												</div>
											</div>
										</div>
										<div class="accordion-group">
											<div class="accordion-heading">
												<a class="accordion-toggle" data-toggle="collapse" data-parent="##siteAccordian" href="##panel2">Workshops</a>
											</div>
											<div id="panel2" class="accordion-body collapse">
												<div class="accordion-inner">
													<cfinclude template="#request.pathToUI#/lists/workshops.cfm">
												</div>
											</div>
										</div> --->
										<!--- <div class="accordion-group">
											<div class="accordion-heading">
												<a class="accordion-toggle" data-toggle="collapse" data-parent="##siteAccordian" href="##panel3">Orders</a>
											</div>
											<div id="panel3" class="accordion-body collapse">
												<div class="accordion-inner">
													<cfinclude template="#request.pathToUI#/lists/orders.cfm">
												</div>
											</div>
										</div>
									</div>
								
								</cfif> --->
				
							</div>
						</div>
					</div>
				</div>
				<div class="modal-footer">
					<a href="?view=#returnView#&#passbackQueryString#" class="btn btn-success">Cancel</a>
					<!--- <cfif site.getSite_ID() gt 0 AND site.getDeleted() eq 0>
						<a class="btn" href="?action=archiveOrganization&view=#returnView#&Organization_ID=#organization.getOrganization_ID()#">Archive</a>
					<cfelseif organization.getOrganization_ID() gt 0 AND organization.getArchived() eq 1>
						<a class="btn" href="?action=unarchiveOrganization&view=#returnView#&Organization_ID=#organization.getOrganization_ID()#">Unarchive</a>
					</cfif> --->
					<button type="submit" name="saveButton" class="btn btn-success">Save</button>
				</div>
			</div>
			
		</cfform>
	
	</cfoutput>

<cfelse>

	<div class="alert alert-info notFound">Site not found or insufficient permissions to perform this operation</div>

</cfif>